package com.ujoku.service.impl;

import com.labillusion.core.database.base.mybatis.dao.BaseDao;
import com.labillusion.core.database.base.mybatis.service.base.impl.BaseServiceImpl;
import com.labillusion.core.platform.exception.ResourceNotFoundException;
import com.labillusion.core.util.HttpUtils;
import com.ujoku.context.ActionContext;
import com.ujoku.context.constants.ContextConstants;
import com.ujoku.dao.MemberDao;
import com.ujoku.domain.Member;
import com.ujoku.domain.admin.Parameter;
import com.ujoku.service.CdnService;
import com.ujoku.service.MemberService;
import com.ujoku.service.MessageService;
import com.ujoku.service.admin.ParametersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 14-10-19.
 */
@Service
public class MemberServiceImpl extends BaseServiceImpl<Member> implements MemberService<Member> {

    @Autowired
    private MemberDao dao;

    @Autowired
    private ParametersService parametersService;

    @Value("${settings.customer.upload}")
    private String memberPhotoPath;

    @Value("${settings.web.domain}")
    private String basePath;

    @Override
    protected BaseDao<Member> getBaseDao() {
        return dao;
    }

    @Autowired
    private MessageService messageService;

    @Autowired
    private ActionContext actionContext;

    @Autowired
    private CdnService cdnService;

    @Override
    public Member selectByUserName(String userName) {
        Map<String, Object> query = new HashMap<String, Object>();
        query.put("user_name",userName);
        Member member = this.selectOne(query);

        if(member == null)
            return  null;

        Parameter p = parametersService.getParameterByKey(ContextConstants.MEMBER_LEVEL + member.getLevel());
        if(p != null)
        {
            member.setLevelName(p.getMark());
            member.setDiscount(Double.parseDouble(p.getValue()));
        }
        p = parametersService.getParameterByKey(ContextConstants.MEMBER_UPGRADE + member.getLevel());
        if (p != null) {
            member.setNextLevelName(p.getMark());
            member.setUpgradeCost(Double.parseDouble(p.getValue()));
        }

        member.setUnreadCounts(getUnreadCountsById(member) > 0 ? getUnreadCountsById(member) : 0);

        return member;
    }

    @Override
    public Member selectByWxId(String wxId, String type){
        if (!"WEB".equals(type) && !"APP".equals(type) || wxId == null){
            throw new ResourceNotFoundException("can not get wxId or type");
        }
        Map<String, Object> query = new HashMap<String, Object>();
        if("WEB".equals(type))
            query.put("webWxId",wxId);
        if("APP".equals(type))
            query.put("appWxId",wxId);
        Member member = this.selectOne(query);

        if(member == null)
            return  null;

        Parameter p = parametersService.getParameterByKey(ContextConstants.MEMBER_LEVEL + member.getLevel());
        if(p != null)
        {
            member.setLevelName(p.getMark());
            member.setDiscount(Double.parseDouble(p.getValue()));
        }
        p = parametersService.getParameterByKey(ContextConstants.MEMBER_UPGRADE + member.getLevel());
        if (p != null) {
            member.setNextLevelName(p.getMark());
            member.setUpgradeCost(Double.parseDouble(p.getValue()));
        }

        member.setUnreadCounts(getUnreadCountsById(member) > 0 ? getUnreadCountsById(member) : 0);

        return member;
    }

    @Override
    public int addFriend(Integer memberId, Integer friendId){
        Map<String,Object> query = new HashMap<>();
        query.put("memberId",memberId);
        query.put("friendId",friendId);
        return dao.addFriend(query);
    }

    @Override
    public int removeFriend(Integer memberId, Integer friendId){
        Map<String,Object> query = new HashMap<>();
        query.put("memberId",memberId);
        query.put("friendId",friendId);

        return dao.removeFriend(query);
    }

    @Override
    public List<Member> getMyFriendList(Map<String, Object> query){

        return dao.getMyFriendList(query);
    }

    @Override
    public int getMyFriendCount(Integer memberId){
        Map<String, Object> query = new HashMap<>();
        query.put("memberId", memberId);

        return dao.getMyFriendCount(query);
    }

    @Override
    public int isFriendShip(Integer memberId,Integer friendId){
        Map<String, Object> query = new HashMap<>();
        query.put("memberId", memberId);
        query.put("friendId", friendId);

        return dao.getMyFriendCount(query);
    }

    @Override
    public void setMemberPortrait(Member member,HttpServletRequest request) {
        if (member.isPortrait()) {
            String portrait = cdnService.portraitBuilder(HttpUtils.getBasePath(request) + memberPhotoPath + member.getUser_id() + "_head.jpg", member.getUpdateTime().getTime());
            member.setHeadPortraitUrl(portrait);
        }else
            member.setHeadPortraitUrl(basePath + "/images/front/head_img.png");
    }

    @Override
    public String getMemberPortrait(boolean portrait, int memberId, Date portraitUpdateTime) {
        if (portrait)
            return cdnService.portraitBuilder(HttpUtils.getBasePath(actionContext.getRequest()) + memberPhotoPath + memberId + "_head.jpg" , portraitUpdateTime.getTime());
        else
            return basePath + "/images/front/head_img.png";
    }

    @Override
    public int getUnreadCountsById(Member member){
        Map<String, Object> query = new HashMap<>();
        query.put("toMemberId", member.getUser_id());
        query.put("status",1);
        return messageService.selectCount(query);
    }

    @Override
    public void setLevelName(Member member) {
        Parameter p = parametersService.getParameterByKey(ContextConstants.MEMBER_LEVEL + member.getLevel());
        if (p != null) {
            member.setLevelName(p.getMark());
            member.setDiscount(Double.parseDouble(p.getValue()));
        }
        p = parametersService.getParameterByKey(ContextConstants.MEMBER_UPGRADE + member.getLevel());
        if (p != null) {
            member.setNextLevelName(p.getMark());
            member.setUpgradeCost(Double.parseDouble(p.getValue()));
        }
    }

    @Override
    public int updateStatus(Member member){

        return dao.updateStatus(member);
    }
}
